package monster.com.cvh.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.GetRequest;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import monster.com.cvh.R;
import monster.com.cvh.adapter.ResumeShopAdapter;
import monster.com.cvh.bean.ResumeShopBean;
import monster.com.cvh.fragment.base.PermissionFragment;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: 13580
 * @data: 2018/4/10
 * @function:简历商城TabFragment
 */
public class ResumeShopFragment extends PermissionFragment {
    public static final String RESUME_TYPE = "mResumeTypeKey";
    private static final String TAG = "ResumeShopFragment";
    @BindView(R.id.rv_fragment_resume_shop)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private ResumeShopAdapter mAdapter;
    private String mResumeType = "";
    private List<ResumeShopBean.DataBean> mData;
    private int mCurrentPage = 0;

    public static ResumeShopFragment newInstance(String type) {
        Bundle args = new Bundle();
        args.putString(RESUME_TYPE, type);
        ResumeShopFragment fragment = new ResumeShopFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mResumeType = args.getString(RESUME_TYPE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_resume_shop;
    }

    @Override
    protected void initData() {
    }

    private void initViewData() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        mAdapter = new ResumeShopAdapter(mData);
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.view_empty_layout,null,true));
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    protected void initEvent() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                updateData();
            }
        });
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                loadMoreData(++mCurrentPage);
            }
        });
        mRefreshLayout.autoRefresh();
    }

    private void loadMoreData(int page_index) {
        String token = SPUtils.getString(getContext(), MyConstant.TOKEN, "");
        GetRequest request = OkGo.<String>get(MyConstant.GET_RESUME_TEMPLATE_LIST).tag(this)
                .params(MyConstant.RESUME_TYPE, mResumeType)
                .params(MyConstant.PAGE_INDEX, page_index);
       if (!token.isEmpty()){
           request.params(MyConstant.TOKEN, token);
       }
            request.execute(new StringCallback() {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    try {
                        ResumeShopBean bean = new Gson().fromJson(s, ResumeShopBean.class);
                        if (bean.getCode() == 1) {
                            //判断有无数据
                            List<ResumeShopBean.DataBean> data = bean.getData();
                            if (data.size() > 0) {
                                mData = data;
                                mAdapter.addData(mData);
                            } else {
                                --mCurrentPage;
                                showToastShort(R.string.refresh_no_more_data);
                            }
                        } else {
                            --mCurrentPage;
                            showToastShort(R.string.net_load_failed);
                        }
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                        --mCurrentPage;
                        showToastShort(R.string.net_load_failed);
                    } finally {
                        mRefreshLayout.finishLoadmore();
                    }
                }

                @Override
                public void onError(Call call, Response response, Exception e) {
                    super.onError(call, response, e);
                    --mCurrentPage;
                    showToastShort(R.string.net_load_failed);
                    mRefreshLayout.finishLoadmore();
                }
            });

    }


    private void updateData() {
        //重置页数
        mCurrentPage = 0;
        GetRequest request = OkGo.<String>get(MyConstant.GET_RESUME_TEMPLATE_LIST).tag(this)
                .params(MyConstant.RESUME_TYPE, mResumeType)
                .params(MyConstant.PAGE_INDEX, mCurrentPage);
        String token = SPUtils.getString(getContext(), MyConstant.TOKEN, "");
        if (!token.isEmpty()){
            request.params(MyConstant.TOKEN, token);
        }
        request.execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ResumeShopBean bean = new Gson().fromJson(s, ResumeShopBean.class);
                            if (bean.getCode() == 1) {
                                mData = bean.getData();
                                initViewData();
                            } else {
                                showToastShort(R.string.net_load_failed);
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            showToastShort(R.string.net_load_failed);
                        } finally {
                            if (mRefreshLayout != null) {
                                mRefreshLayout.finishRefresh();
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        showToastShort(R.string.net_load_failed);
                        mRefreshLayout.finishRefresh();
                    }
                });
    }

}
